{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false
   },
   "source": [
    "# Wifi Example\n",
    "\n",
    "In this notebook, we show how to connect to a WiFi network.\n",
    "\n",
    "For boards equipped with a USB interface, a WiFi dongle can be plugged \n",
    "into the board. Specifically a RALink WiFi dongle commonly used with \n",
    "Raspberry Pi kits is connected into the board. Using Linux calls and \n",
    "Python functions, we will determine the unique name of the dongle and \n",
    "then create a network entry for a known ssid/password pair.\n",
    "\n",
    "For boards equipped with onboard WiFi module, we can follow the same\n",
    "process as well.\n",
    "\n",
    "References:\n",
    "http://www.canakit.com/raspberry-pi-wifi.html\n",
    "\n",
    "### 1. Create WiFi instance\n",
    "Make sure:\n",
    "1. The USB WiFi module has been plugged in, or \n",
    "2. There is already an embedded WiFi module on board (e.g. `Ultra96`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pynq.lib import Wifi\n",
    "\n",
    "port = Wifi()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Connect to a WIFI link\n",
    "Type in the SSID and password as instructed. It may take a while to establish the connection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "hide_input": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type in the SSID:iphone_hotspot\n",
      "Type in the password:12345678\n"
     ]
    }
   ],
   "source": [
    "ssid = input(\"Type in the SSID:\")\n",
    "pwd = input(\"Type in the password:\")\n",
    "port.connect(ssid, pwd)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Test connection\n",
    "Ping 10 packets, then the following cell stops automatically. Notice there\n",
    "are only a few websites that accept `ping`.\n",
    "\n",
    "The following cell assumes the default wireless interface `wlan0`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PING www.yahoo.com(media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4)) from 2600:380:4455:f5aa:d9be:7d77:2574:fb61 wlan0: 56 data bytes\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=1 ttl=50 time=278 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=2 ttl=50 time=136 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=3 ttl=50 time=182 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=4 ttl=50 time=92.7 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=5 ttl=50 time=132 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=6 ttl=50 time=130 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=7 ttl=50 time=146 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=8 ttl=50 time=136 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=9 ttl=50 time=141 ms\n",
      "64 bytes from media-router-fp1.prod1.media.vip.gq1.yahoo.com (2001:4998:c:1023::4): icmp_seq=10 ttl=50 time=132 ms\n",
      "\n",
      "--- www.yahoo.com ping statistics ---\n",
      "10 packets transmitted, 10 received, 0% packet loss, time 9012ms\n",
      "rtt min/avg/max/mdev = 92.740/151.020/278.769/47.309 ms\n"
     ]
    }
   ],
   "source": [
    "! ping -I wlan0 www.yahoo.com -c 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Reset connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "port.reset()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
